home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacWorld 1997 November
/
Macworld (1997-11).dmg
/
Shareware World
/
Comms & Internet
/
BetterTelnet
/
documentation
/
Goodies
/
icrtest.c
next >
Wrap
C/C++ Source or Header
|
1997-07-08
|
5KB
|
165 lines
/* icrtest
* Produces a test pattern on an ICR compatible display. Demonstrates and provides
* example code for writing ICR programs.
*
* National Center for Supercomputing Applications
* University of Illinois, Urbana-Champaign
*
* by Tim Krauskopf
* This program is in the public domain.
*/
#include <stdio.h>
int
xdim=0,ydim=0; /* size of image on disk */
char
*malloc(),
*testimage,
rgb[768]; /* storage for a palette */
main(argc,argv)
int argc;
char *argv[];
{
register int i,j;
register char *p;
puts("Creating test pattern");
xdim = 150;
ydim = 100;
if (NULL == (testimage = malloc(xdim*ydim)))
exit(1);
/*
* Make the test image in a strange pattern.
*/
p = testimage;
for (i=0; i<ydim; i++)
for (j=0; j<xdim; j++) {
*p++ = 50 + (((i & 0xfffffff8) * (j & 7))>>2);
}
puts("Displaying test pattern with the Interactive Color Raster protocol");
rimage(0); /* display remote image with [palette] */
}
/*****************************************************************************/
/* rimage
* Remote display of the image using the ICR.
* Just print the codes to stdout using the protocol.
*/
rimage(usepal)
int usepal;
{
int i,j,newxsize;
char *space,*thisline,*thischar;
register unsigned char c;
/*
* Open the window with the W command.
*/
(void)printf("\033^W;%d;%d;%d;%d;0;test window^",0,0,xdim,ydim);
/*
* If a palette should be used, send it with the M command.
*/
if (usepal) {
(void)printf("\033^M;0;256;768;test window^"); /* start map */
thischar = rgb;
for (j=0; j<768; j++) {
c = *thischar++;
if (c > 31 && c < 123) {
putchar(c);
}
else {
putchar((c>>6)+123);
putchar((c & 0x3f) + 32);
}
}
}
/*
* Send the data for the image with RLE encoding for efficiency.
* Encode each line and send it.
*/
space = malloc(ydim+100);
thisline = testimage;
for (i = 0; i < ydim; i++) {
newxsize = rleit(thisline,space,xdim);
thisline += xdim; /* increment to next line */
(void)printf("\033^R;0;%d;%d;%d;test window^",i,1,newxsize);
thischar = space;
for (j = 0; j < newxsize; j++) {
/***********************************************************************/
/* Encoding of bytes:
*
* 123 precedes #s 0-63
* 124 precedes #s 64-127
* 125 precedes #s 128-191
* 126 precedes #s 192-255
* overall: realchar = (specialchar - 123)*64 + (char-32)
* specialchar = r div 64 + 123
* char = r mod 64 + 32
*/
/***********************************************************************/
c = *thischar++; /* get byte to send */
if (c > 31 && c < 123) {
putchar(c);
}
else {
putchar((c>>6)+123);
putchar((c & 0x3f) + 32);
}
}
}
free(space);
}
/********************************************************************/
/* rleit
*
* Compress the data to go out with a simple run-length encoded scheme.
*
*/
rleit(buf,bufto,len)
int len;
char *buf,*bufto;
{
register char *p,*q,*cfoll,*clead;
char *begp;
int i;
p = buf;
cfoll = bufto; /* place to copy to */
clead = cfoll + 1;
begp = p;
while (len > 0) { /* encode stuff until gone */
q = p + 1;
i = len-1;
while (*p == *q && i+120 > len && i) {
q++;
i--;
}
if (q > p + 2) { /* three in a row */
if (p > begp) {
*cfoll = p - begp;
cfoll = clead;
}
*cfoll++ = 128 | (q-p); /* len of seq */
*cfoll++ = *p; /* char of seq */
len -= q-p; /* subtract len of seq */
p = q;
clead = cfoll+1;
begp = p;
}
else {
*clead++ = *p++; /* copy one char */
len--;
if (p > begp + 120) {
*cfoll = p - begp;
cfoll = clead++;
begp = p;
}
}
}
/*
* fill in last bytecount
*/
if (p > begp)
*cfoll = 128 | (p - begp);
else
clead--; /* dont need count position */
return((int)(clead - bufto)); /* how many stored as encoded */
}